package com.game.HUD;

import javax.microedition.khronos.opengles.GL10;

import com.game.R;

import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;
import system.Device;
import system.Graphics;

public class ShowPlayerHP extends ShowGUIItem {
	
	private static int m_iTexBloodBackgroundID;
	private static int m_iTexBloodID;
	private static int m_iBloodBgWidth = 160;
	private static int m_iBloodBgHeight = 21;
	private static int m_iBloodWidth = 145;
	private static int m_iBloodHeight = 20;
	private static TexSquare m_texBloodSquare;
	private static TexSquare m_texBloodBgSquare;
	private static Texture m_texture;
	private static float m_fSrcHeight;
	private static float m_fSrcWidth;
	private static GL10 gl;
	private static int MAX_HP_SUPER_HERO = 1000;
	private static int m_iSuperHeroHP = 800;
	private static final short BLOOD_OFFSET = 12;
	
	public static void Init()
	{		
		m_fSrcWidth = Device.GetWidth();
		m_fSrcHeight = Device.GetHeight();
		gl = Graphics.GetGraphics();
		m_texture = new Texture();
		
		m_iTexBloodID = m_texture.initTexture(R.drawable.blood);
		m_iTexBloodBackgroundID = m_texture.initTexture(R.drawable.blood_bg);
		
		Device.DisableScaleFactor();
		m_texBloodSquare = new TexSquare(m_iBloodWidth, m_iBloodHeight);
		m_texBloodBgSquare = new TexSquare(m_iBloodBgWidth, m_iBloodBgHeight);
		Device.EnableScaleFactor();
	}
	
	public static void Update(){
		MAX_HP_SUPER_HERO = s_level.getMaxPlayerHP();
		m_iSuperHeroHP = s_level.getCurrentPlayerHP();
	}
	
	public static void Render(){
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		// Blood background
		gl.glLoadIdentity();
		gl.glTranslatef(- (m_fSrcWidth - m_iBloodBgWidth)/2, (m_fSrcHeight - m_iBloodBgHeight)/2 - 4, 0);
		m_texture.loadTexture(m_iTexBloodBackgroundID);
		m_texBloodBgSquare.Render();
		
		
		// Blood
		float x = ((float) m_iSuperHeroHP / MAX_HP_SUPER_HERO);
		float bloodWidth = (int)(x*m_iBloodWidth);
		
		gl.glLoadIdentity();
		gl.glTranslatef(-(m_fSrcWidth - bloodWidth)/2 + BLOOD_OFFSET, (m_fSrcHeight - m_iBloodHeight)/2 - 4, 0);
		
		m_texBloodSquare.SetTexCoord(new float[]{
			0.0f, 0.0f,
			x, 0.0f,
			x, 1.0f,
			0.0f, 1.0f
		});
		m_texBloodSquare.SetWidth(bloodWidth);
		m_texture.loadTexture(m_iTexBloodID);
		m_texBloodSquare.Render();
		
		gl.glPopMatrix();
	}
}
